home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / fork2_test.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  6.8 KB  |  242 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # This is a test with uses 5 processes to insert, update and select from
  4. # two tables.
  5. # One inserts records in the tables, one updates some record in it and
  6. # the last 3 does different selects on the tables.
  7. # Er, hmmm..., something like that :^)
  8. # Modified to do crazy-join, α la Nasdaq.
  9. #
  10. # This test uses the old obsolete mysql interface. For a test that uses
  11. # DBI, please take a look at fork_big.pl
  12.  
  13. $opt_loop_count=10000; # Change this to make test harder/easier
  14.  
  15. ##################### Standard benchmark inits ##############################
  16.  
  17. use Mysql;
  18. use Getopt::Long;
  19. use Benchmark;
  20.  
  21. package main;
  22.  
  23. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  24.   $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  25. $opt_host=""; $opt_db="test";
  26.  
  27. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
  28.        "skip-delete", "verbose","fast-insert","lock-tables","debug","fast",
  29.        "force") || die "Aborted";
  30. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$Mysql::db_errstr=$opt_force=undef;  # Ignore warnings from these
  31.  
  32. print "Testing 10 multiple connections to a server with 1 insert/update\n";
  33. print "and 8 select connections and one ALTER TABLE.\n";
  34.  
  35.  
  36. @testtables = qw(bench_f21 bench_f22 bench_f23 bench_f24 bench_f25);
  37. $numtables = $#testtables;    # make emacs happier
  38. $dtable = "directory";
  39. ####  
  40. ####  Start timeing and start test
  41. ####
  42.  
  43. $start_time=new Benchmark;
  44. if (!$opt_skip_create)
  45. {
  46.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  47.   $Mysql::QUIET = 1;
  48.   foreach $table (@testtables) {
  49.       $dbh->Query("drop table $table");
  50.   }
  51.   $dbh->Query("drop table $dtable");
  52.   $Mysql::QUIET = 0;
  53.  
  54.   foreach $table (@testtables) {
  55.       print "Creating table $table in database $opt_db\n";
  56.       $dbh->Query("create table $table".
  57.           " (id int(6) not null,".
  58.           " info varchar(32),".
  59.           " marker timestamp,".
  60.           " primary key(id))")
  61.       or die $Mysql::db_errstr;
  62.   }
  63.   print "Creating directory table $dtable in $opt_db\n";
  64.   $dbh->Query("create table $dtable (id int(6), last int(6))")
  65.       or die $Mysql::db_errstr;
  66.   # Populate directory table
  67.   for $i ( 0 .. $numtables ) {
  68.       $dbh->Query("insert into $dtable values($i, 0)");
  69.   }
  70.   $dbh=0;            # Close handler
  71. }
  72. $|= 1;                # Autoflush
  73.  
  74. ####
  75. #### Start the tests
  76. ####
  77.  
  78. #$test_index = 0;
  79.  
  80. test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
  81. test_2() if (($pid=fork()) == 0); $work{$pid}="simple1";
  82. test_3() if (($pid=fork()) == 0); $work{$pid}="funny1";
  83. test_2() if (($pid=fork()) == 0); $work{$pid}="simple2";
  84. test_3() if (($pid=fork()) == 0); $work{$pid}="funny2";
  85. test_2() if (($pid=fork()) == 0); $work{$pid}="simple3";
  86. test_3() if (($pid=fork()) == 0); $work{$pid}="funny3";
  87. test_2() if (($pid=fork()) == 0); $work{$pid}="simple4";
  88. test_3() if (($pid=fork()) == 0); $work{$pid}="funny4";
  89. alter_test() if (($pid=fork()) == 0); $work{$pid}="alter";
  90.  
  91. $errors=0;
  92. while (($pid=wait()) != -1)
  93. {
  94.   $ret=$?/256;
  95.   print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  96.   $errors++ if ($ret != 0);
  97. }
  98.  
  99. if (!$opt_skip_delete && !$errors)
  100. {
  101.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  102.   foreach $table (@testtables) {
  103.       $dbh->Query("drop table $table");
  104.   }
  105. }
  106. print ($errors ? "Test failed\n" :"Test ok\n");
  107.  
  108. $end_time=new Benchmark;
  109. print "Total time: " .
  110.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  111.  
  112. exit(0);
  113.  
  114. #
  115. # Insert records in the ?? tables the Nasdaq way
  116.  
  117. sub test_1
  118. {
  119.     my ($dbh,$table,$tmpvar,$rows,$found,$i);
  120.  
  121.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  122.     $tmpvar=1;
  123.     $rows=$found=0;
  124.     for ($i=0 ; $i < $opt_loop_count; $i++)
  125.     {
  126.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  127.     # Nasdaq step 1:
  128.     $sth=$dbh->Query("select id,last from $dtable where id='$tmpvar'")
  129.         or die "Select directory row: $Mysql::db_errstr\n";
  130.     # Nasdaq step 2:
  131.     my ($did,$dlast) = $sth->FetchRow
  132.         or die "Fetch directory row: $Mysql::db_errstr\n";
  133.     $dlast++;
  134.     $sth=$dbh->Query("INSERT into $testtables[$did]".
  135.              " VALUES($dlast,'This is entry $dlast',NULL)")
  136.         || die "Got error on insert table $testtable[$did]:". 
  137.         " $Mysql::db_errstr\n";
  138.     # Nasdaq step 3 - where my application hangs
  139.     $sth=$dbh->Query("update $dtable set last='$dlast' where id='$tmpvar'")
  140.         or die "Updating directory for table $testtable[$did]:".
  141.         " Mysql::db_errstr\n";
  142.     $rows++;
  143.     }
  144.     $dbh=0;
  145.     print "Test_1: Inserted $rows rows\n";
  146.     exit(0);
  147. }
  148.  
  149. #
  150. # Nasdaq simple select
  151. #
  152.  
  153. sub test_2
  154. {
  155.     my ($dbh,$id,$tmpvar,$rows,$found,$i);
  156.  
  157.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  158.     $rows=$found=0;
  159.     $tmpvar=1;
  160.     for ($i=0 ; $i < $opt_loop_count ; $i++)
  161.     {
  162.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  163.     $sth=$dbh->Query("select a.id,a.info from $testtables[$tmpvar] as a,".
  164.              "$dtable as d".
  165.              " where a.id=d.last and $i >= 0")
  166.         || die "Got error select max: $Mysql::db_errstr\n";
  167.     if ((@row = $sth->FetchRow()) && defined($row[0]))
  168.     {
  169.         $found++;
  170.     }
  171.     }
  172.     $dbh=0;
  173.     print "Test_2: Found $found rows\n";
  174.     exit(0);
  175. }
  176.  
  177.  
  178. #
  179. # Nasdaq not-so-simple select
  180. #
  181.  
  182. sub test_3
  183. {
  184.     my ($dbh,$id,$tmpvar,$rows,$i);
  185.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  186.     $rows=0;
  187.     $tmpvar ||= $numtables;
  188.     for ($i=0 ; $i < $opt_loop_count ; $i++)
  189.     {
  190.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  191.     $id1 = ($tmpvar+1) % $numtables;
  192.     $id2 = ($id1+1) % $numtables;
  193.     $id3 = ($id2+1) % $numtables;
  194.     $sth = $dbh->Query("SELECT greatest(a.id, b.id, c.id), a.info".
  195.                " FROM $testtables[$id1] as a,".
  196.                " $testtables[$id2] as b,".
  197.                " $testtables[$id3] as c,".
  198.                " $dtable as d1, $dtable as d2, $dtable as d3".
  199.                " WHERE ".
  200.                " d1.last=a.id AND d2.last=b.id AND d3.last=c.id".
  201.                " AND d1.id='$id1' AND d2.id='$id2'".
  202.                " AND d3.id='$id3'")
  203.         or die "Funny select: $Mysql::db_errstr\n";
  204.     $rows+=$sth->numrows;
  205.     }
  206.     $dbh=0;
  207.     print "Test_3: Found $rows rows\n";
  208.     exit(0);
  209. }
  210.  
  211. #
  212. # Do an ALTER TABLE every 20 seconds
  213. #
  214.  
  215. sub alter_test
  216. {
  217.     my ($dbh,$count,$old_row_count,$row_count,$id,@row,$sth);
  218.  
  219.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  220.     $id=$count=$row_count=0; $old_row_count= -1;
  221.  
  222.     # Execute the test as long as we get more data into the table
  223.     while ($row_count != $old_row_count)
  224.     {
  225.       sleep(10);
  226.       $sth=$dbh->Query("ALTER TABLE $testtables[$id] modify info varchar(32)") or die "Couldn't execute ALTER TABLE\n";
  227.       $sth=0;
  228.       $id=($id+1) % $numtables;
  229.  
  230.       # Test if insert test has ended
  231.       $sth=$dbh->query("select count(*) from $testtables[0]") or die "Couldn't execute count(*)\n";
  232.       @row = $sth->FetchRow();
  233.       $old_row_count= $row_count;
  234.       $row_count=$row[0];
  235.       $count++;
  236.     }
  237.     $dbh=0;
  238.     print "alter: Executed $count ALTER TABLE commands\n";
  239.     exit(0);
  240. }
  241.